package com.limh.gobang.view

import android.graphics.Point

/**
 * @function：判断输赢
 * @author： limh
 * @date： 2018/4/22
 * Copyright @2018 Corpration Name
 */
object CheckInFive {
    val MAX_COUNT_LINE = 5
    /**
     * 检查横向是否赢
     */
    fun checkHorizontal(x: Int, y: Int, points: ArrayList<Point>): Boolean {
        var count = 1
        for (i in 1..MAX_COUNT_LINE) {
            //左
            if (points.contains(Point(x - i, y))) {
                count++
            } else {
                break
            }

        }
        if (count == MAX_COUNT_LINE) {
            return true
        }
        for (i in 1..MAX_COUNT_LINE) {
            //右
            if (points.contains(Point(x + i, y))) {
                count++
            } else {
                break
            }
        }
        if (count == MAX_COUNT_LINE) {
            return true
        }
        return false
    }

    /**
     * 检查纵向是否赢
     */
    fun checkVertical(x: Int, y: Int, points: ArrayList<Point>): Boolean {
        var count = 1
        for (i in 1..MAX_COUNT_LINE) {
            //上
            if (points.contains(Point(x, y - i))) {
                count++
            } else {
                break
            }

        }
        if (count == MAX_COUNT_LINE) {
            return true
        }
        for (i in 1..MAX_COUNT_LINE) {
            //下
            if (points.contains(Point(x, y + i))) {
                count++
            } else {
                break
            }
        }
        if (count == MAX_COUNT_LINE) {
            return true
        }
        return false
    }

    /**
     * 左斜
     */
    fun checkLeftDiagonal(x: Int, y: Int, points: ArrayList<Point>): Boolean {
        var count = 1
        for (i in 1..MAX_COUNT_LINE) {
            //左上
            if (points.contains(Point(x + i, y - i))) {
                count++
            } else {
                break
            }

        }
        if (count == MAX_COUNT_LINE) {
            return true
        }
        for (i in 1..MAX_COUNT_LINE) {
            //左下
            if (points.contains(Point(x - i, y + i))) {
                count++
            } else {
                break
            }
        }
        if (count == MAX_COUNT_LINE) {
            return true
        }
        return false
    }

    /**
     * 右斜
     */
    fun checkRightDiagonal(x: Int, y: Int, points: ArrayList<Point>): Boolean {
        var count = 1
        for (i in 1..MAX_COUNT_LINE) {
            //右上
            if (points.contains(Point(x - i, y - i))) {
                count++
            } else {
                break
            }

        }
        if (count == MAX_COUNT_LINE) {
            return true
        }
        for (i in 1..MAX_COUNT_LINE) {
            //右下
            if (points.contains(Point(x + i, y + i))) {
                count++
            } else {
                break
            }
        }
        if (count == MAX_COUNT_LINE) {
            return true
        }
        return false
    }
}